import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '../../config/database';

// 礼物接口
interface GiftAttributes {
  id: string;
  name: string; // 礼物名称
  price: number; // 礼物价格
  type?: number | null; // 礼物类型
  description?: string | null; // 礼物描述
  icon?: string | null; // 礼物图标
  createdAt?: Date;
}

// 创建时可选的属性
interface GiftCreationAttributes extends Optional<GiftAttributes, 'id' | 'createdAt'> {}

// 礼物模型类
class Gift extends Model<GiftAttributes, GiftCreationAttributes> implements GiftAttributes {
  declare id: string;
  declare name: string;
  declare price: number;
  declare type: number | null;
  declare description: string | null;
  declare icon: string | null;
  
  // 时间戳
  declare readonly createdAt: Date;
}

// 初始化模型
Gift.init(
  {
    id: {
      type: DataTypes.STRING,
      primaryKey: true,
      allowNull: false
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    },
    price: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      validate: {
        min: 0
      }
    },
    type: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    icon: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  },
  {
    sequelize,
    tableName: 'gifts',
    modelName: 'Gift',
    timestamps: true,
    updatedAt: false,
    indexes: [
      {
        fields: ['name']
      },
      {
        fields: ['price']
      }
    ]
  }
);

export default Gift;